package com.mtconnect.filter;

/**
 * Created by Administrator on 2017/9/22.
 */

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤url请求，对请求方法进行过滤
 */
@WebFilter(urlPatterns = {"/*"})
public class RequestMethodFilter implements Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request1 = (HttpServletRequest) request;
        if (!request1.getMethod().equalsIgnoreCase("GET")) {
            HttpServletResponse response1 = (HttpServletResponse) response;
            response1.sendError(HttpServletResponse.SC_BAD_REQUEST, "request method error");
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
